package com.tbit.main.util;


import lombok.extern.slf4j.Slf4j;

import java.awt.*;
import java.awt.datatransfer.Clipboard;
import java.awt.datatransfer.StringSelection;
import java.awt.event.KeyEvent;
import java.io.BufferedReader;
import java.io.IOException;
import java.io.InputStream;
import java.io.InputStreamReader;
import java.net.HttpURLConnection;
import java.net.URL;
import java.text.DecimalFormat;
import java.util.ArrayList;
import java.util.List;
import java.util.Scanner;

import static com.tbit.main.util.DateTimeUtil.*;

/**
 * @author LMY
 * @create 2022-05-13 10:21
 */
@Slf4j
public class Robot {
    public static void main(String[] args) throws Exception {
        //chatSoftWare(1);
        countDown(2);
    }


    /**
     * 按照软件类型执行不同操作
     *
     * @param softwareType 1=聊天软件, 2=lol
     * @throws AWTException
     * @throws InterruptedException
     */
    private static void chatSoftWare(int softwareType) throws AWTException, InterruptedException {
        java.awt.Robot robot = new java.awt.Robot();
        Scanner sc = new Scanner(System.in);
        System.out.println("请选择模式（1：温柔模式，2：暴力模式，3：恐怖模式）");
        int type = sc.nextInt();
        String url = null;
        if (type != 3) {
            url = String.format("http://fuck.lvkaixuan.top/public/index/index/getFuckText?type=%s", type);
        } else {
            url = "https://api.1314.cool/words/api.php?return=txt";
        }
        System.out.println("请输入要轰炸的次数:");
        int count = sc.nextInt();
        System.out.println("五秒后进行轰炸，请打开聊天窗口");
        Thread.sleep(5000);
        //获取系统粘贴板
        Clipboard clipboard = Toolkit.getDefaultToolkit().getSystemClipboard();
        List<String> list = new ArrayList();
        for (int i = 1; i <= count; i++) {
            //间隔0.1秒
            robot.delay(100);
            String line1 = doGet(url);

            String line = line1.replace("<br>", "\n");
            if (list.contains(line)) {
                //重复
                i--;
                System.out.println("轰炸内容重复，重新请求");
                continue;
            }
            list.add(line);
            System.out.println(String.format("轰炸内容%s:%s", i, line));
            clipboard.setContents(new StringSelection(line), null);
            robot.keyPress(KeyEvent.VK_CONTROL);
            robot.keyPress(KeyEvent.VK_V);
            robot.keyRelease(KeyEvent.VK_CONTROL);
            robot.keyRelease(KeyEvent.VK_V);
            //间隔0.1秒
            robot.delay(100);
            //回车
            robot.keyPress(KeyEvent.VK_ENTER);
            robot.keyRelease(KeyEvent.VK_ENTER);
            if (softwareType == 2) {
                //间隔0.1秒打开聊天框
                robot.delay(100);
                //回车
                robot.keyPress(KeyEvent.VK_ENTER);
                robot.keyRelease(KeyEvent.VK_ENTER);
            }
        }
        System.out.println("轰炸结束，祝您生活愉快！");
    }

    /**
     * http Get请求
     *
     * @param httpUrl
     * @return
     */
    public static String doGet(String httpUrl) {
        HttpURLConnection connection = null;
        InputStream inputString = null;
        BufferedReader bufferedReader = null;
        String result = null;
        try {
            URL url = new URL(httpUrl);
            connection = (HttpURLConnection) url.openConnection();
            connection.setRequestMethod("GET");
            //超时时间
            connection.setConnectTimeout(15000);
            //读取远程返回的数据时间
            connection.setReadTimeout(60000);
            //发送请求
            connection.connect();
            //获取输入流
            if (connection.getResponseCode() == 200) {
                inputString = connection.getInputStream();
                //封装输入流is，指定字符集
                bufferedReader = new BufferedReader(new InputStreamReader(inputString, "UTF-8"));
                //存放数据
                StringBuilder stringBuilder = new StringBuilder();
                String temp = null;
                while ((temp = bufferedReader.readLine()) != null) {
                    stringBuilder.append(temp);
                }
                result = stringBuilder.toString();
            }
        } catch (Exception e) {
        } finally {
            // 关闭资源
            if (null != bufferedReader) {
                try {
                    bufferedReader.close();
                } catch (IOException e) {
                    log.error(e.getMessage(), e);
                }
            }
            if (null != bufferedReader) {
                try {
                    inputString.close();
                } catch (IOException e) {
                    log.error(e.getMessage(), e);
                }
            }
            // 关闭远程连接
            connection.disconnect();
        }
        return result;
    }


    /**
     * 倒计时
     */
    private static void countDown(int softwareType) throws AWTException, InterruptedException {
        java.awt.Robot robot = new java.awt.Robot();
        Scanner sc = new Scanner(System.in);
        System.out.println("请输入要轰炸的次数:");
        System.out.println("五秒后进行轰炸，请打开聊天窗口");
        Thread.sleep(5000);
        int i = 0;
        //获取系统粘贴板
        Clipboard clipboard = Toolkit.getDefaultToolkit().getSystemClipboard();
        List<String> list = new ArrayList();
        while (true) {
            //间隔0.1秒
            robot.delay(5000);
            DecimalFormat df = new DecimalFormat("0.00");//设置保留位数
            df.format((float) getTime(getNowTime(), getNowTimeYMD() + " 17:50:00") / 3600);
            String line1 = "距离" + getNowTimeYMD() + " 17:50:00下班，还有：" + getTime(getNowTime(), getNowTimeYMD() + " 17:50:00") + "秒；" +
                    getTime(getNowTime(), getNowTimeYMD() + " 17:50:00") / 60 + "分钟；" + df.format((float) getTime(getNowTime(), getNowTimeYMD() + " 17:50:00") / 3600) + "小时";

            String line = line1.replace("<br>", "\n");
            if (list.contains(line)) {
                //重复
                System.out.println("轰炸内容重复，重新请求");
            }
            list.add(line);
            i++;
            System.out.println(String.format("轰炸内容%s:%s", i, line));
            clipboard.setContents(new StringSelection(line), null);
            robot.keyPress(KeyEvent.VK_CONTROL);
            robot.keyPress(KeyEvent.VK_V);
            robot.keyRelease(KeyEvent.VK_CONTROL);
            robot.keyRelease(KeyEvent.VK_V);
            //间隔0.1秒
            robot.delay(1000);
            //回车
            robot.keyPress(KeyEvent.VK_ENTER);
            robot.keyRelease(KeyEvent.VK_ENTER);
            if (softwareType == 2) {
                //间隔0.1秒打开聊天框
                robot.delay(10000);
                //回车
                robot.keyPress(KeyEvent.VK_ENTER);
                robot.keyRelease(KeyEvent.VK_ENTER);
            }
        }

        //System.out.println("轰炸结束，祝您生活愉快！");
    }
}
